<template>
    <div>
        <h1>食谱信息添加</h1>
        <table class="table">
             <tbody>
                <tr>
                    <td>食谱标题:</td>
                    <td>
                        <input type="text" v-model="data.foodTitle">
                    </td>
                </tr>
                <tr>
                    <td>食谱类型:</td>
                    <td>
                        <select v-model="data.typeName" >
                            <option value="">请选择</option>
                            <option v-for="m in xia" :value="m.typeName">{{m.typeName}}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>食谱封面:</td>
                    <td>
                        <input type="file" @change="file">
                        <img :src="'https://localhost:7211'+data.foodImg" alt="" style="width: 100px; height: 100px;">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="添加" @click="Add">
                        <a href="Show">返回列表</a>
                    </td>
                    
                </tr>
            </tbody> 
        </table>
    </div>  
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue';
import axios from 'axios';
import Moment from 'moment';
const moment=Moment();
//图片
const file=(e:any)=>{
    var f=e.target.files[0];
    var fd=new FormData();
    fd.append("f1",f);
    axios.post("https://localhost:7211/api/File/TuPian",fd).then(res=>{
        data.value.foodImg=res.data;
    })
}
//下拉框
onMounted(()=>{
    la();
})
const xia=ref([{
    "typeId": 0,
    "typeName": ""
}])
const la=()=>{
    axios.get("https://localhost:7211/api/FoodType/XiaLa",{params:xia.value}).then(res=>{
        xia.value=res.data;
    })
}
//添加
const data=ref({
    "foodId": 0,
    "foodTitle": "",
    "typeName": "",
    "foodImg": ""
})

const Add=()=>{
    axios.post("https://localhost:7211/api/Food/Add",data.value).then(res=>{
        if(res.data>0)
    {
        alert("添加成功！");
        location.href='Showfood';
    }
    else{
        alert("添加失败！");
    }
    })
}
</script>

<style scoped>

</style>